﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;
using System.Xml.Linq;
using System.IO;
using Windows.UI;
using BoolanSlide.RedoUndo;

namespace BoolanElement
{
    public class BooEllipseElement : BooTextShapesBaseElement
    {
        public BooEllipseElement()
        {
        }

        public override async Task<BooUIBase> Render(SaveOriginalElement saveOriginal, SaveForRedoUndo saveForRedoUndo)
        {
            BooEllipse ellipse = new BooEllipse(this, saveForRedoUndo);
            RedoUndoHelper.SaveOriginalStatus(ellipse);
            return ellipse;
        }

        public override async Task<BooUIBase> RenderForThumbNails()
        {
            BooEllipse ellipse = new BooEllipse(this);
            return ellipse;
        }
  
        public override XElement SaveToXElement()
        {
            XElement element = new XElement(typeof(BooEllipseElement).Name);
            element.SetAttributeValue("UniqueID", UniqueID.ToString());
            element.Add(ElementTransform.Save());
            element.Add(SaveShapeStyle());
            element.Add(SaveData());

            return element;
        }

        public static BooEllipseElement InitElementStyle()
        {
            BooEllipseElement ellipseElement = new BooEllipseElement();
            ellipseElement.ElementTransform.Rotate.Angle = 0;
            ellipseElement.ElementTransform.Rotate.CenterX = 0;
            ellipseElement.ElementTransform.Rotate.CenterY = 0;

            ellipseElement.ElementTransform.Scale.ScaleX = 1;
            ellipseElement.ElementTransform.Scale.ScaleY = 1;
            ellipseElement.ElementTransform.Scale.CenterX = 0;
            ellipseElement.ElementTransform.Scale.CenterY = 0;

            ellipseElement.ElementTransform.Translate.X = 0;
            ellipseElement.ElementTransform.Translate.Y = 0;

            ellipseElement.ElementTransform.Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 0);

            ellipseElement.ElementTransform.Width = 200;
            ellipseElement.ElementTransform.Height = 200;

            ellipseElement.TextMarginLeft = 0;
            ellipseElement.TextMarginTop = 0;
            ellipseElement.TextMaxHeight = 200;
            ellipseElement.TextWidth = 200;
            
            ellipseElement.TextAlignment = SerializeCast.StringToTextAlignment("CenterCenter");

            ellipseElement.ShapeStyle = SerializeCast.StringToTextBoxStyle("NoBorderBKG");
            ellipseElement.BorderColor = Color.FromArgb(255, 0, 0, 0);
            ellipseElement.BorderWidth = 0;

            ellipseElement.ShapeFillColor = Color.FromArgb(255, 24, 122, 169);
            ellipseElement.ShapeWidth = 200;
            ellipseElement.ShapeHeight = 200;

            ellipseElement.ControlPanelTop = 0;
            ellipseElement.ControlPanelLeft = 0;

            ellipseElement.Text = "";

            return ellipseElement;
        }

        public override void LoadFromXElement(XElement element)
        {
            XElement tranformElement = element.Element("Transform");
            XElement dataElement = element.Element("Data");
            XElement styleElement = element.Element("Style");

            ElementTransform.Load(tranformElement);
            LoadShapeStyle(styleElement);
            LoadData(dataElement);
        }

        public override BooElement Clone()
        {
            BooEllipseElement element = new BooEllipseElement();
            element.ElementTransform = this.ElementTransform.Clone();

            element.TextMarginLeft = this.TextMarginLeft;
            element.TextMarginTop = this.TextMarginTop;
            element.TextMaxHeight = this.TextMaxHeight;
            element.TextWidth = this.TextWidth;
           
            element.TextAlignment = this.TextAlignment;

            element.ShapeStyle = this.ShapeStyle;
            element.BorderColor = Color.FromArgb(this.BorderColor.A,this.BorderColor.R,this.BorderColor.G,this.BorderColor.B);
            element.BorderWidth = this.BorderWidth;

            element.ShapeFillColor = Color.FromArgb(this.ShapeFillColor.A,this.ShapeFillColor.R,this.ShapeFillColor.G,this.ShapeFillColor.B);
            element.ShapeWidth = this.ShapeWidth;
            element.ShapeHeight = this.ShapeHeight;

            element.ControlPanelTop = this.ControlPanelTop;
            element.ControlPanelLeft = this.ControlPanelLeft;
            element.Text = this.Text;

            return element;

        }

        public override void Load(BooTextShapesBaseElement shapesElement)
        {
            BooEllipseElement ele = shapesElement as BooEllipseElement;
            this.ElementTransform = ele.ElementTransform;

            this.TextMarginLeft = ele.TextMarginLeft;
            this.TextMarginTop = ele.TextMarginTop;
            this.TextMaxHeight = ele.TextMaxHeight;
            this.TextWidth = ele.TextWidth;

            this.TextAlignment = ele.TextAlignment;

            this.ShapeStyle = ele.ShapeStyle;
            this.BorderColor = Color.FromArgb(ele.BorderColor.A, ele.BorderColor.R, ele.BorderColor.G, ele.BorderColor.B);
            this.BorderWidth = ele.BorderWidth;

            this.ShapeFillColor = Color.FromArgb(ele.ShapeFillColor.A, ele.ShapeFillColor.R, ele.ShapeFillColor.G, ele.ShapeFillColor.B);
            this.ShapeWidth = ele.ShapeWidth;
            this.ShapeHeight = ele.ShapeHeight;

            this.ControlPanelTop = ele.ControlPanelTop;
            this.ControlPanelLeft = ele.ControlPanelLeft;
            this.Text = ele.Text;
        }
    }
}
